<?php
namespace Swiftx\View;
use Swiftx\Interfaces\Http\Response;
use Swiftx\Interfaces\Ioc\Container;

class Handle {

    /**
     * 视图配置
     * @var array
     */
    protected $config;

    /**
     * 依赖注入容器
     * @var Container
     */
    protected $container;

    /**
     * 构造视图组件
     * @param Container $container = provider(Application)
     * @param array $config = provider(Oday@Admin::Config)
     */
    public function __construct(Container $container,array $config){
        $this->config = $config['View'];
        $this->container = $container;
    }

    /**
     * 创建视图
     * @param string $name
     * @return Response
     */
    public function create(string $name):Response{
        if(!isset($this->config[$name]))
            die('视图不存在');
        $config = $this->config[$name];
        $prototype = $this->container->create($config['Prototype']);
        unset($config['Prototype']);
        foreach($config as $key => $value)
            call_user_func([$prototype,'set'.$key], $value);
        return $prototype;
    }


}